package lk.icta.common.dao.impl;

import lk.icta.common.dao.PresidentDAO;
import lk.icta.common.entity.President;
import lk.icta.user.entity.User;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.Query;
import java.sql.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: root
 * Date: 10/17/12
 * Time: 12:09 PM
 * To change this template use File | Settings | File Templates.
 */
public class PresidentDAOImpl extends BaseDAO implements PresidentDAO {
    @Transactional(propagation = Propagation.MANDATORY)
    public void addPresident(President president, User user) {
        logger.debug("Adding President");
        em.persist(president);
    }

    @Transactional(propagation = Propagation.MANDATORY)
    public void updatePresident(President president, User user) {
        logger.debug("Updating president");
        em.merge(president);
    }

    @Transactional(propagation = Propagation.MANDATORY)
    public void deletePresident(President president, User user) {
        logger.debug("Delete a president {} by {}", president.getFullEnName(), user.getUsername());
        em.remove(president);
        em.flush();
    }

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public List<President> getCurrentPresident() {
        Query q = em.createNamedQuery("findCurrentPresident");
        return q.getResultList();
    }
}
